#pragma once

#include <google/protobuf/service.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/TcpServer.h>
#include <muduo/net/InetAddress.h>
#include <google/protobuf/descriptor.h>
#include <unordered_map>
#include <string>

struct ServiceInfo {
    google::protobuf::Service *service;
    std::unordered_map<std::string, const google::protobuf::MethodDescriptor *> method_map;
};

class RpcServer {
public:

    void NotifyService(google::protobuf::Service *service);

    void Run();

private:
    // EventLoop 
    muduo::net::EventLoop m_event_loop_;
    // service 记录
    std::unordered_map<std::string, ServiceInfo> m_service_map;

    // 连接或断开回调
    void onConnection(const muduo::net::TcpConnectionPtr &conn);

    // 消息读写回调
    void onMessage(const muduo::net::TcpConnectionPtr &conn,
                   muduo::net::Buffer *buffer,
                   muduo::Timestamp time);

    // RPC方法调用回调
    void sendRpcResponse(const muduo::net::TcpConnectionPtr &conn,
                         google::protobuf::Message *response);
};